package priv.lirenhe.dissertation.executor;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import priv.lirenhe.dissertation.bdb.url_queue.NotUsedUrlQueue;
import priv.lirenhe.dissertation.bdb.util.UrlQueUtils;
import priv.lirenhe.dissertation.cacher.Cacher;
import priv.lirenhe.dissertation.cacher.CacherManager;
import priv.lirenhe.dissertation.component.crawler_robot.crawler_robot_executor.CrawlerRobotExecutor;
import priv.lirenhe.dissertation.component.data_parser.data_parser_executor.DataParserExecutor;
import priv.lirenhe.dissertation.service.impl.WebPageServiceImpl;
import priv.lirenhe.dissertation.service.impl.WebUrlServiceImpl;
import priv.lirenhe.dissertation.utils.UrlUtil;

public class ComponentsExecutor {
	/*
	public void execute(Map<String,String> map){
		Map<String,String> realUrlAndWebContentOfMap = null;
		realUrlAndWebContentOfMap = getRealUrlAndWebContentOfMap();
		
	}
	*/
	/*
	private boolean setRealUrlAndWebContentOfMapInMysql(Map<String,String> map){
		
		boolean flag = false;
		WebUrlServiceImpl webUrlServiceImpl = null;
		if(!map.isEmpty()){
			webUrlServiceImpl = new WebUrlServiceImpl();
			WebUrl webUrl = new WebUrl();
			
			
			
			
		}
		
		
		
		
		return flag;
	}
	
	private Map<String,String> getRealUrlAndWebContentOfMap(){
		
		CrawlerRobotExecutor crawlerRobotExecutor = new CrawlerRobotExecutor();
		Map<String,String> realUrlAndWebContentOfMap = null;
//		realUrlAndWebContentOfMap = crawlerRobotExecutor.execute();
		
		System.out.println();
		
		return realUrlAndWebContentOfMap;
	}
	
	private Map<String,String> makeWebUrlInDb(){
		
		Map<String,String> urlParamsOfMap = new HashMap<String,String>();
//		urlParamsOfMap.put(key, value)
		return null;
	}
	*/
	
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","mybatis-config.xml"});
		WebUrlServiceImpl webUrlServiceImpl = null;
		webUrlServiceImpl = (WebUrlServiceImpl) ac.getBean("WebUrlServiceImpl");
		
		WebPageServiceImpl webPageServiceImpl = null;
		webPageServiceImpl = (WebPageServiceImpl) ac.getBean("WebPageServiceImpl");
		
		//子线程启动爬虫
		ClawlerThread clawlerThread = new ClawlerThread();
		clawlerThread.startThread();
		
		CacherManager cacherManager = new CacherManager();
		Map<String, Cacher> dataCacherOfMap = null;
		Cacher webContentCacher = null;
		String realUrl = null;
		String webContent = null;
		
		Map<Object,Object> dataParserExecutorMap = null;
		List<String> urlsOfList = null;
		String webContentInText = null;
		String webTitle = null;
		
		Map<String,Map<String,String>> webUrlMap = new HashMap<String,Map<String,String>>();
		Map<String,String> paramsOfWebUrlMap = null;
		
		int cacheFlushNum = 0;
		
		NotUsedUrlQueue notUserdUrlQueue = UrlQueUtils.getNotUsedUrlQueue();//不能打开了数据库而一直不关闭
		while(true){
			
			System.out.println(Thread.currentThread().getName()+ ":扫描缓存" + ++cacheFlushNum);
			
			if(!cacherManager.isEmpty()){
				
				System.out.println(Thread.currentThread().getName()+ ":缓存不为空");
				
				dataCacherOfMap = cacherManager.getCacheAll();
				Map<String,String> webPageMap = new HashMap<String,String>();
				for(Map.Entry<String, Cacher> entry:dataCacherOfMap.entrySet()){
					realUrl = entry.getKey();
					webContentCacher = entry.getValue();
					webContent = (String)webContentCacher.getDatas();
					
					System.out.println("ComponentsExecutor realUrl="+realUrl);
					
					DataParserExecutor dataParserExecutor = new DataParserExecutor(realUrl, webContent);
					dataParserExecutorMap = dataParserExecutor.getDataAfterParser();
					if(dataParserExecutor != null){
						urlsOfList = (List<String>) dataParserExecutorMap.get("urlOfList");
						webContentInText = (String) dataParserExecutorMap.get("webContentInText");
						webTitle = (String) dataParserExecutorMap.get("webTitle");
					}
					
					System.out.println("urlsOfList.size()="+urlsOfList.size());
					System.out.println("webContentInText.length()="+webContentInText.length());
					
					if(urlsOfList != null && !urlsOfList.isEmpty()){
						
						for(String str:urlsOfList){
							if(!notUserdUrlQueue.isContainsValue(str)){
								
								System.out.println("将解析的url放入notUsed BDB中");
								
								notUserdUrlQueue.setNotUsedUrl(str);
								/*
								if(notUserdUrlQueue.getNotUsedUrlSize() <= 0){
									System.out.println("notUserdUrlQueue.getNotUsedUrlSize() <= 0");
									notUserdUrlQueue.syncBDB();
								}
								*/
								System.out.println("解析的url="+str);
								if(str.length()>255){
									System.out.println("url长度大于255"+str);
								}
								
								paramsOfWebUrlMap = new HashMap<String,String>();
								paramsOfWebUrlMap.put("webUrlRealUrl",str);
								paramsOfWebUrlMap.put("webUrlMainfatherurl", UrlUtil.resolveMainUrl(str));
								paramsOfWebUrlMap.put("webUrlFatherurl",realUrl);
								paramsOfWebUrlMap.put("webUrlEffective", "effective");
								paramsOfWebUrlMap.put("webUrlHasused","notHasUsed");
								webUrlMap.put(str, paramsOfWebUrlMap);
							}else{
								System.out.println("在BDB中重复url="+str);
								continue;
							}
						}
					}
					
					//关闭BDB数据库
					if(notUserdUrlQueue != null){
						
						System.out.println("数据库notUserdUrlQueue BDB中的数据size:"+notUserdUrlQueue.getNotUsedUrlSize());
						
						UrlQueUtils.syncNotUsedUrlQueueBDB();//持久化到磁盘，等到以后分开写的时候可以在finally中持久化
					}
					
					System.out.println("webUrlMap.size()="+webUrlMap.size());
					
					try {
						System.out.println("入库webUrlMap=====");
						int i = 0;
						i = webUrlServiceImpl.insertUrlSelective(webUrlMap);
						if(i>=0){
							System.out.println("清空webUrlMap=====");
							webUrlMap.clear();
						}
						
						//内容入库
						System.out.println("内容入库webPageMap======");
						webPageMap.put("webPageRealurl",realUrl);
						webPageMap.put("webPageTitle", webTitle);
						webPageMap.put("webPageContentCode",webContent);
						webPageMap.put("webPageContentText", webContentInText);
						int x = 0;
						x = webPageServiceImpl.insertPageSelective(webPageMap);
						if(x >= 0){
							webPageMap.clear();
						}
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}
				
				//清空缓存
				dataCacherOfMap.clear();
			}else{
				try {
					
					System.out.println(Thread.currentThread().getName() + ":缓存为空,睡眠五秒");
					
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}


/*	private Map<String,String> getDataFromCache(){
	
	
	
}
*/	

